package com.meilai.project.controller.app.appController;

import com.meilai.project.dto.CommonResult;
import com.meilai.project.dto.app.MobileLoginDTO;
import com.meilai.project.dto.app.SendMobileCodeDTO;
import com.meilai.project.dto.app.SignInParamForApp;
import com.meilai.project.service.middleware.CaptchaService;
import com.meilai.project.service.system.LoginService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author 张驰
 * @date 2022/3/15 22:52
 */
@Api(value = "app登录控制器", tags = {"app登录"})
@RestController
@RequestMapping("api/app/login")
public class AppLoginController {

    @Autowired
    private CaptchaService captchaService;
    @Autowired
    private LoginService loginService;

    /**
     * 发送短信验证码
     */
    @ApiOperation(value = "发送短信验证码", notes = "请求发送短信验证码")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @PostMapping("send-sms")
    public CommonResult sendAPPSMSCode(@RequestBody @Valid @ApiParam(name="发送短信验证码请求对象",value="传入json格式",required=true) SendMobileCodeDTO dto,
                                       BindingResult bindingResult) {
        if (bindingResult.hasErrors()) return new CommonResult().validateFailed(bindingResult);
        return captchaService.sendAPPSMSCode(dto);
    }

    /**
     * 手机登录
     */
    @ApiOperation(value = "手机号登录", notes = "请求手机号登录", response = SignInParamForApp.class)
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @PostMapping("sign-in-mobile")
    public CommonResult loginByMobile(
            HttpServletRequest request,
            @RequestBody @Valid @ApiParam(name="手机号登录请求对象",value="传入json格式",required=true) MobileLoginDTO dto,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) return new CommonResult().validateFailed(bindingResult);
        return new CommonResult().success(loginService.appLogin(request, dto));
    }
}
